package com.uenpay.oem.util.camera;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

public class CameraCropBorderView extends View {
	/**
	 * 绘制的矩形的宽度
	 */
	private int mWidth = 640;
	/**
	 * 绘制的矩形的高度
	 */
	private int mHeight = 640;
	/**
	 * 边框的颜色，默认为白色
	 */
	private int mBorderColor = Color.parseColor("#FFFFFF");
	/**
	 * 边框以外的颜色
	 */
	private int mFillColor = Color.parseColor("#e0000000");
	/**
	 * 边框的宽度 单位dp
	 */
	private int mBorderWidth = 1;
	/**
	 * border的画笔
	 */
	private Paint mPaint;
	/**
	 * fill的画笔
	 */
	private Paint mPaintFill;
	/**
	 * 绘制的矩形的范围
	 */
	private Rect rect = new Rect();
	/**
	 * 第一次绘制时取得rect的范围
	 */
	private boolean isFirst = true;

	public static int left = 0;
	public static int right = 0;
	public static int top = 0;
	public static int bottom = 0;

	private int bncaptureWidth = 0;
	private int bnToggleWidth = 0;

	public CameraCropBorderView(Context context, int captureWidth, int toggleWidth) {
		this(context, null);
		bncaptureWidth = captureWidth;
		bnToggleWidth = toggleWidth;
		Log.i("CameraCropBorderView", "bncaptureWidth : " + bncaptureWidth);
	}

	public CameraCropBorderView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public CameraCropBorderView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mBorderWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mBorderWidth, getResources().getDisplayMetrics());
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setColor(mBorderColor);
		mPaint.setStrokeWidth(mBorderWidth);
		mPaint.setStyle(Style.STROKE);

		mPaintFill = new Paint();
		mPaintFill.setAntiAlias(true);
		mPaintFill.setColor(mFillColor);
		mPaintFill.setAlpha(100);
		mPaintFill.setStyle(Style.FILL);
	}

	public Rect getRect() {
		return rect;
	}

	@Override
	protected void onDraw(Canvas canvas) {
//		if (isFirst) {
//			int srceenW = getWidth();
//			int screenH = getHeight();
//			mWidth = mHeight = srceenW > screenH ? screenH : srceenW;
//			int left = (srceenW - mWidth) / 2 + 10;
//			int top = 2*(mWidth - left) - 10;
//			int right = mWidth - 10 ;
//			int bottom = 10;
//			Log.i("draw", "left :" + left + "top :" + top + "right : " + right + "bottom : " + bottom);
//			rect.set(left, top, right, bottom);
//			isFirst = false;
//		}
		if (isFirst) {
			int srceenW = getWidth();
			int screenH = getHeight();
			Log.i("draw", "srceenW :" + srceenW + "screenH :" + screenH);
			mWidth = mHeight = srceenW > screenH ? screenH : srceenW;
			left = bnToggleWidth;
			right = srceenW - bncaptureWidth;
			top = (screenH - (2*srceenW - 6*left)/3)/2;
			bottom = top + (2*srceenW - 6*left)/3;
			Log.i("draw", "left :" + left + "top :" + top + "right : " + right + "bottom : " + bottom);
			rect.set(left, top, right, bottom);
			isFirst = false;
		}
		// 绘制外边框
		canvas.drawRect(rect, mPaint);
		//绘制上边
		canvas.drawRect(0, 0, getWidth(), rect.top, mPaintFill);
		//绘制下边
		canvas.drawRect(0, rect.bottom, getWidth(), getHeight(), mPaintFill);
		//绘制左边
		canvas.drawRect(0, rect.top, rect.left, rect.bottom, mPaintFill);
		//绘制右边
		canvas.drawRect(rect.right, rect.top, getWidth(), rect.bottom, mPaintFill);
	}

}
